//
//  YOUfanAuthItemHeaderView.m
//  NewYoufanApp
//
//  Created by 九月 on 6/27/24.
//

#import "YOUfanAuthItemHeaderView.h"

#import "TTotheAuthDataListController.h"

@interface YOUfanAuthItemSubView : UIView

@property (strong, nonatomic) UILabel *hajimiTitleLabel;
@property (strong, nonatomic) UITextField *jiucunTextFeild;

@property (strong, nonatomic) NSString *cunhuaValue;

@property (assign, nonatomic) NSInteger heiyouIndex;

@property (assign, nonatomic) YOUfanAuthItemType ayiguType;

@end

@implementation YOUfanAuthItemSubView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.left = 0;
        
        self.hajimiTitleLabel = [UILabel new];

        self.hajimiTitleLabel.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        self.hajimiTitleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        [self addSubview:self.hajimiTitleLabel];
        [self.hajimiTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self);
            make.top.equalTo(@(hajimiPadding16));
            make.height.mas_equalTo(20);
        }];
        
        UIView *habibi = [[UIView alloc] init];
        habibi.layer.cornerRadius = 4;
        habibi.layer.masksToBounds = YES;
        habibi.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
        habibi.layer.borderColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1].CGColor;
        habibi.layer.borderWidth = 0.5;
        [self addSubview:habibi];
        [habibi mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
            make.leading.equalTo(self.hajimiTitleLabel);
            make.width.mas_equalTo(self.width);
            make.top.mas_equalTo(self.hajimiTitleLabel.mas_bottom).offset(hajimiPadding12);
        }];
        
        UIImageView *ayiguImgView = [[UIImageView alloc] init];
        [habibi addSubview:ayiguImgView];
        [ayiguImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(@(-hajimiPadding08));
            make.centerY.equalTo(habibi);
            make.size.mas_equalTo(CGSizeMake(hajimiPadding16, hajimiPadding16));
        }];
        
        self.jiucunTextFeild = [UITextField new];
        self.jiucunTextFeild.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
        self.jiucunTextFeild.font = [UIFont systemFontOfSize:hajimiFontOfSize15];
        [habibi addSubview:self.jiucunTextFeild];
        [self.jiucunTextFeild mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(@(hajimiPadding12));
            make.right.mas_equalTo(ayiguImgView.mas_left).offset(-hajimiPadding12);
            make.height.mas_equalTo(30);
            make.centerY.equalTo(habibi);
        }];
    }
    return self;
}


@end


@interface YOUfanAuthItemHeaderView () <UITextFieldDelegate>

@property (strong, nonatomic) NSArray <YOUfanAuthItemSubView *>*ttotheSubViews;


@end

@implementation YOUfanAuthItemHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)habibiOnHiddenInfoAction:(UIButton *)jiucun {
    jiucun.selected = !jiucun.selected;
    self.ttotheIsHidden = jiucun.selected;
}

- (void)setAyiguType:(YOUfanAuthItemType)ayiguType {
    _ayiguType = ayiguType;
    
    NSMutableArray *youfan = [NSMutableArray array];
    for (int i=0; i<self.ttotheValuesArray.count; i++) {
        NSDictionary *ttotheValueDict = self.ttotheValuesArray[i];
        YOUfanAuthItemSubView *jiucun = [[YOUfanAuthItemSubView alloc] init];
        jiucun.heiyouIndex = i;
        jiucun.ayiguType = ayiguType;
        jiucun.hajimiTitleLabel.text = ttotheValueDict[@"title"];
        jiucun.jiucunTextFeild.placeholder = ttotheValueDict[@"place"];
        jiucun.jiucunTextFeild.delegate = self;
        [jiucun.jiucunTextFeild addTarget:self action:@selector(ayiguTextFileChange:) forControlEvents:UIControlEventEditingChanged];
        [youfan addObject:jiucun];
        jiucun.top = i * jiucun.height;
        [self addSubview:jiucun];
        
        if (i==1) {
            
            UIButton *ayigu = [UIButton buttonWithType:UIButtonTypeCustom];
            [ayigu addTarget:self action:@selector(habibiOnHiddenInfoAction:) forControlEvents:UIControlEventTouchUpInside];
            [jiucun addSubview:ayigu];
            [ayigu mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(36, 20));
                make.centerY.equalTo(jiucun.hajimiTitleLabel);
                make.trailing.equalTo(jiucun);
            }];
            
            UILabel *heiyou = [UILabel new];
            heiyou.text = @"是否展示在个人主页";
            heiyou.font = [UIFont systemFontOfSize:hajimiFontOfSize12];
            heiyou.textColor = [UIColor colorWithRed:184/255.0 green:187/255.0 blue:190/255.0 alpha:1];
            [jiucun addSubview:heiyou];
            [heiyou mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(ayigu);
                make.right.mas_equalTo(ayigu.mas_left).offset(-13.5);
            }];
        }
    }
    self.ttotheSubViews = youfan.copy;
    

}



- (void)ayiguTextFileChange:(UITextField *)habibi {
    if (self.ayiguType == YOUfanAuthItem_ZY) {
        if (self.ttotheSubViews.lastObject.jiucunTextFeild == habibi) {
            self.ttotheValue3 = habibi.text;
        }
        else {
            self.ttotheValue2 = habibi.text;
        }
    }
    if (self.ttatheDidInputAuthInfo) self.ttatheDidInputAuthInfo();
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    if (self.ayiguType == YOUfanAuthItem_CL) {
        if (self.ttotheSubViews.firstObject.jiucunTextFeild == textField) {

            TTotheAuthDataListController *ttothe = [TTotheAuthDataListController new];
            ttothe.certificationtype = certificationTypeCart;
            __weak typeof(self) fyweakSelf = self;
            
            ttothe.backBlock = ^(NSString * _Nonnull nameString) {
                __strong typeof(fyweakSelf) fystrongSelf = fyweakSelf;
                textField.text = nameString;
                fystrongSelf.ttotheValue1 = nameString;
                if (fystrongSelf.ttatheDidInputAuthInfo) fystrongSelf.ttatheDidInputAuthInfo();
            };
            
        }
        else {
            NSString *ttotheCar = self.ttotheSubViews.firstObject.jiucunTextFeild.text;
            if (ttotheCar && ttotheCar.length>0) {

            }
            else {
                __weak typeof(self) fyweakSelf = self;
                TTotheAuthDataListController *ttothe = [TTotheAuthDataListController new];
                ttothe.certificationtype = certificationTypeCart;
                ttothe.carString = self.ttotheSubViews.firstObject.jiucunTextFeild.text;
                ttothe.backBlock = ^(NSString * _Nonnull nameString) {
                    textField.text = nameString;
                    __strong typeof(fyweakSelf) fystrongSelf = fyweakSelf;
                    fystrongSelf.ttotheValue2 = nameString;
                    if (fystrongSelf.ttatheDidInputAuthInfo) fystrongSelf.ttatheDidInputAuthInfo();
                };
                
            }
        }
    }
    else if (self.ayiguType == YOUfanAuthItem_ZY) {
        if (self.ttotheSubViews.firstObject.jiucunTextFeild == textField) {
            __weak typeof(self) fyweakSelf = self;
            
        }
        else {
            return YES;
        }
    }
    else if (self.ayiguType == YOUfanAuthItem_SR) {
        __weak typeof(self) fyweakSelf = self;
        if (self.ttotheSubViews.firstObject.jiucunTextFeild == textField) {
            
        }
        else {
            
        }
    }
    else if (self.ayiguType == YOUfanAuthItem_XL) {
        __weak typeof(self) fyweakSelf = self;
        if (self.ttotheSubViews.firstObject.jiucunTextFeild == textField) {
           
        }
        else {
            TTotheAuthDataListController *ttothe = [TTotheAuthDataListController new];
            ttothe.certificationtype = certificationTypeDegree;
            ttothe.backBlock = ^(NSString * _Nonnull nameString) {
                textField.text = nameString;
                __strong typeof(fyweakSelf) fystrongSelf = fyweakSelf;
                fystrongSelf.ttotheValue2 = nameString;
                if (fystrongSelf.ttatheDidInputAuthInfo) fystrongSelf.ttatheDidInputAuthInfo();
            };
        }
    }
    
    return NO;
}

- (BOOL)ttatheIsfull {
    if (self.ayiguType == YOUfanAuthItem_ZY) {
        return self.ttotheValue1.length && self.ttotheValue2.length && self.ttotheValue3.length;
    }
    else {
        return self.ttotheValue1.length && self.ttotheValue2.length;
    }
}

- (NSArray *)ttotheValuesArray {
    NSDictionary *ttothe1;
    NSDictionary *ttothe2;
    NSDictionary *ttothe3;
    if (self.ayiguType == YOUfanAuthItem_XL) {
        ttothe1 = @{@"title":@"我的学历", @"place":@"请选择学历",@"value":@""};
        ttothe2 = @{@"title":@"毕业院校", @"place":@"请选择真实毕业院校（选填）",@"value":@""};
    }
    else if (self.ayiguType == YOUfanAuthItem_ZY) {
        ttothe1 = @{@"title":@"我的职业", @"place":@"请选择职业",@"value":@""};
        ttothe2 = @{@"title":@"所在公司", @"place":@"请填写所在公司（选填）",@"value":@""};
        ttothe3 = @{@"title":@"职务", @"place":@"请填写职务（选填）",@"value":@""};
    }
    else if (self.ayiguType == YOUfanAuthItem_CL) {
        ttothe1 = @{@"title":@"车辆品牌", @"place":@"请选择品牌",@"value":@""};
        ttothe2 = @{@"title":@"车型", @"place":@"请选择车型（选填）",@"value":@""};
    }
    else if (self.ayiguType == YOUfanAuthItem_SR) {
        ttothe1 = @{@"title":@"年收入", @"place":@"请填写年收入",@"value":@""};
        ttothe2 = @{@"title":@"资产", @"place":@"请填写资产（选填）",@"value":@""};
    }
    if (ttothe3.count>0) {
        return @[ttothe1, ttothe2, ttothe3];
    }
    return @[ttothe1, ttothe2];
}

@end
